{% extends "base.html" %}
{% load promgen %}
{% load i18n %}
{% block content %}

{% if project %}
<div class="page-header" v-pre>
  <h1>Project: {{ project.name }}</h1>
</div>

{% breadcrumb project view.button_label %}

<div class="alert alert-danger" role="alert">
  <span class="glyphicon glyphicon-alert" aria-hidden="true"></span>
  Warning. Renaming or Moving a project will reset all labels in Prometheus
</div>
<div class="alert alert-danger" role="alert">
  <span class="glyphicon glyphicon-alert" aria-hidden="true"></span>
  {% trans "Warning. The owner can only be changed by the current owner. The new owner will have the ADMIN role." %}
</div>
{% else %}
<div class="page-header">
  <h1>Register new Project</h1>
</div>
{% breadcrumb service view.button_label %}
{% endif %}

<form action="" method="post">{% csrf_token %}

  <div class="panel panel-primary">
    <table class="table">
      <tr>
        <th>{{form.name.label}}</th>
        <td>
          {% include 'promgen/error_block.html' with warning=form.name.errors only %}
          {{form.name}}
        </td>
      </tr>
      <tr>
        <th>{{form.description.label}}</th>
        <td>
          {% include 'promgen/error_block.html' with warning=form.description.errors only %}
          {{form.description}}
        </td>
      </tr>
      {% if form.owner %}
      <tr>
        <th>{{form.owner.label}}</th>
        <td>
          {% include 'promgen/error_block.html' with warning=form.owner.errors only %}
          {{form.owner}}
        </td>
      </tr>
      {% endif %}
      {% if project.pk %}
      <tr>
        <th>{{form.service.label}}</th>
        <td>
          {% include 'promgen/error_block.html' with warning=form.service.errors only %}
          {{form.service}}
        </td>
      </tr>
      {% endif %}
    </table>
  </div>
  {% include 'promgen/error_block.html' with warning=form.shard.errors only %}

  {# Only show our warning if modifying an existing project #}
  {% if project.pk %}
  <div class="panel panel-danger">
    <div class="panel-heading">
      <span class="glyphicon glyphicon-alert" aria-hidden="true"></span>
      Warning. Moving a project does not move data. Historical data will remain on the previous shard.
      <button class="btn btn-danger btn-xs pull-right" type="button" data-toggle="collapse" data-target="#shard-selector" aria-expanded="false" aria-controls="shard-selector">
        Select shard
      </button>
    </div>
  </div>
  {% endif %}

  {# Color red and collapse by default for existing projects #}
  <div class="panel{% if project.pk %} panel-danger collapse{% else %} panel-primary{% endif %}" id="shard-selector">
    {% include 'promgen/shard_help.inc.html' %}
    <table class="table">
      <tr>
        <th colspan="3">Datasource</th>
        <th>Samples</th>
        <th>Exporters</th>
      </tr>
      {% for shard in shard_list|dictsort:'name' %}
      <tr {% if shard.enabled is False %}class="text-muted" title="Datasource disabled for new registrations" {% endif %}>
        <th v-pre>{{shard.name}}</th>
        <th v-pre>{{shard.url|urlize}}</th>
        <td>
          <input type="radio" name="shard" value="{{shard.id}}" {% if shard.id == project.shard_id %}checked{% elif shard.enabled is False %}disabled{% endif %}>
        </td>
        <td>
          <data-source-usage class="label" shard="{{shard.name}}" metric="samples" max="{{shard.samples}}"></data-source-usage>
        </td>
        <td>
          <data-source-usage class="label" shard="{{shard.name}}" metric="exporters" max="{{shard.targets}}"></data-source-usage>
        </td>
      </tr>
      {% endfor %}
    </table>
  </div>

  <button class="btn btn-primary">{{ view.button_label }}</button>
</form>
{% endblock %}
